package com.ruoyi.common.enums.wecom;

import cn.hutool.core.util.ObjectUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@Getter
@AllArgsConstructor
public enum WecomChatGroupMemberStatusEnum {

    IN_GROUP(0, "在群"),
    QUIT_GROUP(1, "已退群");

    private final Integer code;
    private final String desc;

    private static final Map<Integer, WecomChatGroupMemberStatusEnum> ENUM_MAP;

    static {
        WecomChatGroupMemberStatusEnum[] values = values();
        Map<Integer, WecomChatGroupMemberStatusEnum> map = new HashMap<>(values.length);
        for (WecomChatGroupMemberStatusEnum value : values) {
            map.put(value.getCode(), value);
        }
        ENUM_MAP = Collections.unmodifiableMap(map);
    }

    /**
     * 根据状态码查找枚举
     */
    public static WecomChatGroupMemberStatusEnum getByCode(Integer code) {
        WecomChatGroupMemberStatusEnum enumValue = ENUM_MAP.get(code);
        if (ObjectUtil.isEmpty(enumValue)) {
            throw new RuntimeException("成员状态码有误: " + code);
        }
        return enumValue;
    }

    /**
     * 检查状态码是否有效
     */
    public static boolean isValid(Integer code) {
        return ENUM_MAP.containsKey(code);
    }

    /**
     * 判断成员是否在群
     */
    public static boolean isInGroup(Integer code) {
        return IN_GROUP.getCode().equals(code);
    }

    /**
     * 判断成员是否已退群
     */
    public static boolean isQuitGroup(Integer code) {
        return QUIT_GROUP.getCode().equals(code);
    }
}


